package com.tengju.data.infrastructure.repository.clickhouse.crm;

import com.tengju.data.domain.model.clickhouse.DwsCrmSpuGmvDetailRepository;
import com.tengju.data.domain.model.clickhouse.DwsCrmSpuGmvDetailSumGmv;
import com.tengju.data.domain.model.user.DominationLevelEnum;
import com.tengju.data.domain.model.user.UserIdCode;
import com.tengju.data.infrastructure.persistence.mybatis.dataobject.DwsCrmSpuGmvDetailSumGmvDO;
import com.tengju.data.infrastructure.persistence.mybatis.mapper.clickhouse.user.DwsCrmSpuGmvDetailMapper;
import com.tengju.data.infrastructure.repository.clickhouse.convert.DwsCrmSpuGmvDetailSumGmvConvert;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: 李大仙
 * @Date: 2021/5/6 9:00 下午
 */
@Repository
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class DwsCrmSpuGmvDetailRepositoryImpl implements DwsCrmSpuGmvDetailRepository {

    private final DwsCrmSpuGmvDetailMapper dwsCrmSpuGmvDetailMapper;

    @Override
    public List<DwsCrmSpuGmvDetailSumGmv> getSumGmvList(DominationLevelEnum dominationLevel, List<UserIdCode> idCodeList, List<String> visitDates, List<Integer> months) {
        if (CollectionUtils.isEmpty(idCodeList)) {
            return Collections.emptyList();
        }
        List<Long> idCodes = idCodeList.stream().map(UserIdCode::getValue).collect(Collectors.toList());
        List<DwsCrmSpuGmvDetailSumGmvDO> dwsCrmSpuGmvDetailSumGmvDOList = dwsCrmSpuGmvDetailMapper.querySumGmvList(dominationLevel.getVal(), idCodes, visitDates, months);
        return dwsCrmSpuGmvDetailSumGmvDOList.stream()
                .map(DwsCrmSpuGmvDetailSumGmvConvert::convert)
                .collect(Collectors.toList());
    }

    @Override
    public int getSumGmvPageTotal(DominationLevelEnum dominationLevel, List<UserIdCode> idCodeList, List<String> visitDates) {
        if (CollectionUtils.isEmpty(idCodeList)) {
            return 0;
        }
        List<Long> idCodes = idCodeList.stream().map(UserIdCode::getValue).collect(Collectors.toList());
        return dwsCrmSpuGmvDetailMapper.querySumGmvPageTotal(dominationLevel.getVal(), idCodes, visitDates);
    }
}
